
COMMON_OBJ = minicore.o malloc.o showmsg.o strlib.o utils.o des.o grfio.o nullpo.o
COMMON_DIR_OBJ = $(COMMON_OBJ:%=../common/obj/%)
COMMON_H = $(shell ls ../common/*.hpp)
COMMON_INCLUDE = -I../common/

RA_INCLUDE = -I../

LIBCONFIG_INCLUDE = -I../../3rdparty/libconfig

RAPIDYAML_OBJ = $(shell find ../../3rdparty/rapidyaml/src/ -type f -name "*.cpp" | sed -e "s/\.cpp/\.o/g" )
RAPIDYAML_DIR_OBJ = $(RAPIDYAML_OBJ:%=obj/%)
RAPIDYAML_AR = ../../3rdparty/rapidyaml/obj/ryml.a
RAPIDYAML_H = $(shell find ../../3rdparty/rapidyaml/src/ -type f -name "*.h*")
RAPIDYAML_INCLUDE = -I../../3rdparty/rapidyaml/src -I../../3rdparty/rapidyaml/ext/c4core/src

YAML_CPP_OBJ = $(shell find ../../3rdparty/yaml-cpp/ -type f -name "*.cpp" | sed -e "s/\.cpp/\.o/g" )
YAML_CPP_DIR_OBJ = $(YAML_CPP_OBJ:%=obj/%)
YAML_CPP_AR = ../../3rdparty/yaml-cpp/obj/yaml-cpp.a
YAML_CPP_H = $(shell find ../../3rdparty/yaml-cpp/ -type f -name "*.h")
YAML_CPP_INCLUDE = -I../../3rdparty/yaml-cpp/include

OTHER_H = ../config/renewal.hpp

MAPCACHE_OBJ = obj_all/mapcache.o

CSV2YAML_OBJ = obj_all/csv2yaml.o

YAML2SQL_OBJ = obj_all/yaml2sql.o

YAMLUPGRADE_OBJ = obj_all/yamlupgrade.o

@SET_MAKE@

#####################################################################
.PHONY : all mapcache csv2yaml yaml2sql yamlupgrade clean help

all: mapcache csv2yaml yaml2sql yamlupgrade

mapcache: obj_all $(MAPCACHE_OBJ) $(COMMON_DIR_OBJ)
	@echo "	LD	$@"
	@@CXX@ @LDFLAGS@ -o ../../mapcache@EXEEXT@ $(MAPCACHE_OBJ) $(COMMON_DIR_OBJ) @LIBS@

csv2yaml: obj_all $(CSV2YAML_OBJ) $(COMMON_DIR_OBJ) $(RAPIDYAML_AR) $(YAML_CPP_AR)
	@echo "	LD	$@"
	@@CXX@ @LDFLAGS@ -o ../../csv2yaml@EXEEXT@ $(CSV2YAML_OBJ) $(COMMON_DIR_OBJ) ../common/obj/database.o $(RAPIDYAML_AR) $(YAML_CPP_AR) @LIBS@

yaml2sql: obj_all $(YAML2SQL_OBJ) $(COMMON_DIR_OBJ) $(RAPIDYAML_AR) $(YAML_CPP_AR)
	@echo "	LD	$@"
	@@CXX@ @LDFLAGS@ -o ../../yaml2sql@EXEEXT@ $(YAML2SQL_OBJ) $(COMMON_DIR_OBJ) $(RAPIDYAML_AR) $(YAML_CPP_AR) @LIBS@

yamlupgrade: obj_all $(YAMLUPGRADE_OBJ) $(COMMON_DIR_OBJ) $(RAPIDYAML_AR) $(YAML_CPP_AR)
	@echo "	LD	$@"
	@@CXX@ @LDFLAGS@ -o ../../yamlupgrade@EXEEXT@ $(YAMLUPGRADE_OBJ) $(COMMON_DIR_OBJ) ../common/obj/database.o $(RAPIDYAML_AR) $(YAML_CPP_AR) @LIBS@

clean:
	@echo "	CLEAN	tool"
	@rm -rf obj_all/*.o ../../mapcache@EXEEXT@ ../../csv2yaml@EXEEXT@ ../../yaml2sql@EXEEXT@ ../../yamlupgrade@EXEEXT@

help:
	@echo "possible targets are 'mapcache' 'csv2yaml' 'yaml2sql' 'yamlupgrade' 'all' 'clean' 'help'"
	@echo "'mapcache'     - mapcache generator"
	@echo "'csv2yaml'     - converts TXT databases to YAML"
	@echo "'yaml2sql'     - converts YAML databases to SQL"
	@echo "'yamlupgrade'  - upgrades YAML databases to latest version"
	@echo "'all'          - builds all above targets"
	@echo "'clean'        - cleans builds and objects"
	@echo "'help'         - outputs this message"

#####################################################################

obj_all:
	-mkdir obj_all

obj_all/%.o: %.cpp $(COMMON_H) $(OTHER_H) $(RAPIDYAML_H) $(YAML_CPP_H)
	@echo "	CXX	$<"
	@@CXX@ @CXXFLAGS@ $(COMMON_INCLUDE) $(RA_INCLUDE) $(LIBCONFIG_INCLUDE) $(RAPIDYAML_INCLUDE) $(YAML_CPP_INCLUDE) @CPPFLAGS@ -c $(OUTPUT_OPTION) $<

# missing common object files
$(COMMON_DIR_OBJ):
	@$(MAKE) -C ../common server

$(RAPIDYAML_AR):
	@$(MAKE) -C ../../3rdparty/rapidyaml

$(YAML_CPP_AR):
	@$(MAKE) -C ../../3rdparty/yaml-cpp
